package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.OrderItem;
import com.ruoyi.system.domain.Price;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.Seat;
import com.ruoyi.system.mapper.OrderItemMapper;
import com.ruoyi.system.mapper.PriceMapper;
import com.ruoyi.system.mapper.RoomMapper;
import com.ruoyi.system.mapper.SeatMapper;
import com.ruoyi.system.service.IOrderItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author ...
 * @date 2022-03-17
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService {
    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private SeatMapper seatMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private PriceMapper priceMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public OrderItem selectOrderItemById(Integer id) {
        return orderItemMapper.selectOrderItemById(id);
    }

    /**
     * 查询订单列表
     *
     * @param orderItem 订单
     * @return 订单
     */
    @Override
    public List<OrderItem> selectOrderItemList(OrderItem orderItem) {
        return orderItemMapper.selectOrderItemList(orderItem);
    }

    /**
     * 新增订单
     *
     * @param orderItem 订单
     * @return 结果
     */
    @Override
    public int insertOrderItem(OrderItem orderItem) {
        orderItem.setCreateTime(DateUtils.getNowDate());
        Room room = roomMapper.selectRoomById(orderItem.getRoomId());
        if (room == null) {
            throw new RuntimeException("当前房间不存在");
        }
        Seat seat = seatMapper.selectSeatById(orderItem.getSeatId());
        if (seat == null) {
            throw new RuntimeException("当前位置不存在");
        }
        orderItem.setRoomName(room.getName());
        Price price = priceMapper.selectPriceById(Long.valueOf(seat.getTypeId()));
        if (price == null) {
            throw new RuntimeException("当前位置价格不存在");
        }
        long l = orderItem.getEndTime().getTime() - orderItem.getStartTime().getTime();
        double ceil = Math.ceil(l /3600000.00);
        int i = (int)ceil;
        int p =Integer.parseInt(price.getPrice())*i;
        orderItem.setPrice(String.valueOf(p));
        orderItem.setStatus("预定中");
        seat.setStatus("已占");
        seatMapper.updateSeat(seat);
        return orderItemMapper.insertOrderItem(orderItem);
    }

    /**
     * 修改订单
     *
     * @param orderItem 订单
     * @return 结果
     */
    @Override
    public int updateOrderItem(OrderItem orderItem) {
        orderItem.setUpdateTime(DateUtils.getNowDate());
        return orderItemMapper.updateOrderItem(orderItem);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemByIds(Integer[] ids) {
        return orderItemMapper.deleteOrderItemByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemById(Integer id) {
        return orderItemMapper.deleteOrderItemById(id);
    }

    @Override
    public List<OrderItem> getMyOrder() {
        int userId = SecurityUtils.getUserId().intValue();
        return orderItemMapper.getMyOrder(userId);
    }
}
